package com.owlsmart.bluecat.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.owlsmart.bluecat.R;
import com.owlsmart.bluecat.api.Api;
import com.owlsmart.bluecat.api.ApiConfig;
import com.owlsmart.bluecat.api.TitleCallback;
import com.owlsmart.bluecat.entity.LoginResponse;
import com.owlsmart.bluecat.utils.StringUtils;

import java.util.HashMap;

public class LoginActivity extends BaseActivity {

    private EditText etAccount;
    private EditText etPwd;
    private Button btnLogin;

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        etAccount = findViewById(R.id.et_account);
        etPwd = findViewById(R.id.et_pwd);
        btnLogin = findViewById(R.id.btn_login);
    }

    @Override
    protected void initDate() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = etAccount.getText().toString().trim();
                String pwd = etPwd.getText().toString().trim();
                login(account, pwd);
            }
        });
    }

    private void login(String account, String pwd) {
        if (StringUtils.isEmpty(account)) {
            showToast("没有输入账号，请输入账号吧");
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            showToast("还没输入密码，输入一下密码哦");
            return;
        }
        navigateToWithFlag(HomeActivity.class,
                Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

        // Map params = new HashMap();
        // HashMap<String, Object> params = new HashMap<>();
        // params.put("mobile", account);
        // params.put("password", pwd);
        // Api.config(ApiConfig.LOGIN, params).postRequest(this, new TitleCallback() {
        //     @Override
        //     public void onSuccess(final String res) {
        //         Log.e("onSuccess", res);
        //         Gson gson = new Gson();
        //         LoginResponse loginResponse = gson.fromJson(res, LoginResponse.class);
        //         if (loginResponse.getCode() == 0) {
        //             String token = loginResponse.getToken();
        //             saveStringToSp("token", token);
        //             // navigateTo(HomeActivity.class); 仅仅做一个跳转，不做其他工作
        //             // 以下两行代码，清除以前的task堆栈(也就是Activity)，创建新的堆栈跳转到新的Activity
        //             navigateToWithFlag(HomeActivity.class,
        //                     Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        //
        //             showToastSync("登录成功");
        //         } else {
        //             showToastSync("登录失败");
        //         }
        //
        //
        //     }
        //
        //     @Override
        //     public void onFailure(Exception e) {
        //
        //     }
        // });

    }


}